//
//  PlayViewController.m
//  口袋
//
//  Created by dllo on 15/5/29.
//  Copyright (c) 2015年 口袋. All rights reserved.
//

#import "PlayViewController.h"
#import <AudioStreamer.h>
#import "singleton.h"
#import <AVFoundation/AVFoundation.h>
#import <STKAudioPlayer.h>
#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height
@interface PlayViewController ()
@property (nonatomic, retain)STKAudioPlayer *audio;
@property (nonatomic, retain)UIButton *buttonStatue;
@property (nonatomic, assign)NSInteger tap;
@property (nonatomic, assign)UISlider *slider;
@property (nonatomic, assign)NSInteger i;
@property (nonatomic, assign)NSInteger j;
@property (nonatomic, assign)UILabel *titleLabel;
@property (nonatomic, retain)NSTimer *time;
@property (nonatomic, retain)UILabel *currentTime;
@property (nonatomic, retain)UILabel *songTime;
@end

@implementation PlayViewController
- (void)dealloc
{
    [_slider release];
    [_titleLabel release];
    [_songTime release];
    [_currentTime release];
    [_time release];
    [_audio release];
    [_labelTitle release];
    [_url release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.j = 1;
    }
    return self;
}

- (void)loadView
{
    [super loadView];
    AVAudioSession *session = [AVAudioSession sharedInstance];
    [session setActive:YES error:nil];
    [session setCategory:AVAudioSessionCategoryPlayback error:nil];
    [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(NSIntegerMin, NSIntegerMin) forBarMetrics:UIBarMetricsDefault];
    
    
    //设置播放背景
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    [imageView setImage:[UIImage imageNamed:@"电台背景.png"]];
    [self.view addSubview:imageView];
    [imageView release];
    
    
    
    
    [self.view setBackgroundColor:[UIColor whiteColor]];
    self.buttonStatue = [[UIButton alloc]initWithFrame:CGRectMake(WIDTH * 0.41, HEIGHT * 0.8, WIDTH * 0.21, HEIGHT * 0.12)];
    [self.buttonStatue  addTarget:self action:@selector(buttonStatueAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.buttonStatue];
    [self.buttonStatue  release];
    [self.buttonStatue setImage:[UIImage imageNamed:@"暂停按钮.png"] forState:UIControlStateNormal];
    UIButton *backButton = [[UIButton alloc]initWithFrame:CGRectMake(WIDTH * 0.03, HEIGHT * 0.045, WIDTH * 0.15, HEIGHT * 0.08)];
    [backButton addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    [backButton setImage:[UIImage imageNamed:@"返回按钮.png"] forState:UIControlStateNormal];
    [self.view addSubview:backButton];
    [backButton release];
//    self.audio = [[AudioStreamer alloc]initWithURL:[NSURL URLWithString:self.url]];
    self.audio = [[STKAudioPlayer alloc]init];
    [self.audio playURL:[NSURL URLWithString:self.url]];
    self.titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(WIDTH * 0.08, HEIGHT * 0.14, WIDTH * 0.85, HEIGHT * 0.1)];
    self.titleLabel.textColor = [UIColor colorWithRed:98/255.0 green:190/255.0 blue:255/255.0 alpha:1.0];
    //[lable setBackgroundColor:[UIColor orangeColor]];
    self.titleLabel.font = [UIFont boldSystemFontOfSize:22];
    self.titleLabel.text = self.labelTitle;
    [self.titleLabel setTextAlignment:NSTextAlignmentCenter];
    self.titleLabel.numberOfLines = 0;
    [self.view addSubview:self.titleLabel];
    [self.titleLabel release];
    
    //下一曲button
    UIButton *nextButton = [[UIButton alloc]initWithFrame:CGRectMake(WIDTH * 0.7, HEIGHT * 0.8, WIDTH * 0.21, HEIGHT * 0.12)];
    [nextButton addTarget:self action:@selector(nextAction:) forControlEvents:UIControlEventTouchUpInside];
    [nextButton setImage:[UIImage imageNamed:@"下一曲按钮.png"] forState:UIControlStateNormal];
    [self.view addSubview:nextButton];
    [nextButton release];
    //上一曲
    UIButton *beforeButton = [[UIButton alloc]initWithFrame:CGRectMake(WIDTH * 0.12, HEIGHT * 0.8, WIDTH * 0.21, HEIGHT * 0.12)];
    [beforeButton addTarget:self action:@selector(beforeButton:) forControlEvents:UIControlEventTouchUpInside];
    [beforeButton setBackgroundImage:[UIImage imageNamed:@"上一曲按钮啊.png"] forState:UIControlStateNormal];
    [self.view addSubview:beforeButton];
    [beforeButton release];
    
    
    
    
    //进度条
    self.slider = [[UISlider alloc]initWithFrame:CGRectMake(0, HEIGHT * 0.672, self.view.frame.size.width, HEIGHT * 0.1)];
    [self.view addSubview:_slider];
    self.time = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(progressAction) userInfo:nil repeats:YES];

    [self.slider addTarget:self action:@selector(updateValue:) forControlEvents:UIControlEventValueChanged];
#pragma mark -
#pragma mark 两个时间的label
    self.currentTime = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH * 0.03, HEIGHT * 0.73, 70, 40)];
    self.currentTime.textColor = [UIColor colorWithRed:98/255.0 green:190/255.0 blue:255/255.0 alpha:1.0];
    self.songTime = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH * 0.85, HEIGHT * 0.73, 70, 40)];
    self.songTime.textColor = [UIColor colorWithRed:98/255.0 green:190/255.0 blue:255/255.0 alpha:1.0];
    [self.view addSubview:_currentTime];
    [self.view addSubview:_songTime];
    [_currentTime release];
    [_songTime release];
    
    [_audio resume];
    [_audio release];
}
//progess事件
- (void)progressAction
{
    
    NSInteger currentTimeSec = _audio.progress / 60;
    NSInteger currentTimeMin = (NSInteger)_audio.progress % 60;
    
    NSInteger songTimeSec = _audio.duration / 60;
    NSInteger songTimeMin = (NSInteger)_audio.duration % 60;
    
    self.currentTime.text = [NSString stringWithFormat:@"%ld:%ld", (long)currentTimeSec, currentTimeMin];
    self.songTime.text = [NSString stringWithFormat:@"%ld:%ld", (long)songTimeSec, songTimeMin];
    self.slider.value = _audio.progress / _audio.duration;
    if (_audio.progress > 0) {
        if (_audio.progress > (_audio.duration - 1)) {
            [_time invalidate];
            self.slider.value = 0;
            self.time = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(progressAction) userInfo:nil repeats:YES];
}
    }
}


- (void)assignment
{
    NSString *str = [self.array objectAtIndex:self.i];
//    [self.audio initWithURL:[NSURL URLWithString:str]];
    self.audio = [[STKAudioPlayer alloc]init];
    [self.audio playURL:[NSURL URLWithString:str]];
    
    NSString *title = [self.titleArray objectAtIndex:self.i];
    self.titleLabel.text = title;
    //self.
    
}
//下一曲点击事件
- (void)nextAction:(id)sender
{
    self.i++;
    [self.audio stop];
    if (_i < [self.array count]) {
        [self assignment];
        
    }
    if (_i == [self.array count])
    {
        self.i = 0;
        
        [self assignment];
    }
    [self.audio resume];
    
//    self.audio = [[AudioStreamer alloc]init];
//    self.audio = [[AudioStreamer alloc]initWithURL:[NSURL URLWithString:[self.array objectAtIndex:tap]]];
//    [self.audio start];
}
- (void)beforeButton:(id)sender
{
    
    self.i--;
    [self.audio stop];
    if (_i >= 0) {
        
        [self assignment];
    }
    if (_i < 0)
    {
        self.i = [self.array count] - 1;
        
        [self assignment];
    }
    
    
    [self.audio resume];
    
}

- (void)updateValue:(id)sender
{
    [self.audio seekToTime:self.slider.value * self.audio.duration];
}
- (void)buttonStatueAction:(id)sender
{
    if (self.j == 1) {
        [self.buttonStatue setImage:[UIImage imageNamed:@"播放按钮.png"] forState:UIControlStateNormal];
        [self.audio pause];
        self.j = 2;
    } else if (self.j == 2){
    
        [self.buttonStatue setImage:[UIImage imageNamed:@"暂停按钮.png"] forState:UIControlStateNormal];
        self.j = 1;
        [self.audio resume];
    }
//        else if ([self.audio isPaused]){
//        [self.audio pause];
//        [self.buttonStatue setImage:[UIImage imageNamed:@"暂停按钮.png"] forState:UIControlStateNormal];
//    }
   
}
- (void)backAction:(id)sender
{
    [self.audio stop];
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}




@end
